/*
 * MConfFormuladas.java
 *
 * Created on 16 de marzo de 2007, 11:40 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.metrica.ui.confFlujos.formulada;

import ig.util.ConexionServer;
import ig.util.NoContinuaException;
import ig.util.ejb.IGBean;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author jsanchez
 */
public class MConfFormuladas implements ig.metrica.ui.interfaces.comunes.IModelo{
    private PConfiguracionFlujosFormulada panel=null;
    private ConFlujosFormuladosRemote remote=null;
    private String nombreModelo=null;
    private Integer idModelo=null;
    /** Creates a new instance of MConfFormuladas */
    public MConfFormuladas(PConfiguracionFlujosFormulada panel) {
        this.panel=panel;
    }
    
    private ConFlujosFormuladosRemote lookupConfiguracionBalanceSBBean() {
        try {
            if(remote==null){
                Properties prop = new Properties();
                prop.put( Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);
                
                String Sockec = new ConexionServer().getSocketServer();
                prop.put( Context.PROVIDER_URL, Sockec);
                InitialContext c = new InitialContext(prop);
                remote=((ConFlujosFormuladosRemoteHome) c.lookup("ConFlujosFormuladosBean")).create();
            }
            return remote;
        } catch(javax.naming.NamingException ne) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        } catch(javax.ejb.CreateException ce) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
            throw new RuntimeException(ce);
        } catch (RemoteException ce) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
            throw new RuntimeException(ce);
        }
    }
    
    public void guardar() throws NoContinuaException {
        try {
            this.lookupConfiguracionBalanceSBBean().update(this.panel.getData());
            this.lookupConfiguracionBalanceSBBean().create(this.panel.getData());            
        } catch (Exception e) {
            e.printStackTrace();
            throw new NoContinuaException(e);
        }
    }
    
    public Integer getIdModelo(){
        return this.idModelo;
    }
    public Integer loadModelo(){
        try{
            if(idModelo==null){
//                Modelo m=null;
//                m=ServiceLocator.getInstancia().getModelo(new ModeloPK(this.getNombreModelo()));
//                idModelo=m.getIdModelo(m.getNombre());
                idModelo=this.lookupModeloSBean().findByNombre(this.getNombreModelo()).getId();
            }
            return idModelo;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    ig.metrica.ModeloSRemote modeloSB=null;
    private ig.metrica.ModeloSRemote lookupModeloSBean() {
        try {
            if(modeloSB==null){
                Properties prop = new Properties();
                prop.put( Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);
                String Sockec = new ConexionServer().getSocketServer();
                prop.put( Context.PROVIDER_URL, Sockec);
                
                javax.naming.Context c = new javax.naming.InitialContext(prop);
                Object remote = c.lookup("ModeloSBean");
                ig.metrica.ModeloSRemoteHome rv = (ig.metrica.ModeloSRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, ig.metrica.ModeloSRemoteHome.class);
                modeloSB =rv.create();
            }
            return modeloSB;
        } catch(javax.naming.NamingException ne) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        } catch(javax.ejb.CreateException ce) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
            throw new RuntimeException(ce);
        } catch(java.rmi.RemoteException re) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,re);
            throw new RuntimeException(re);
        }
    }
    
    public Collection buscar() throws NoContinuaException {
        try {
            return this.lookupConfiguracionBalanceSBBean().findByModelo(this.loadModelo());
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }
    
    public void modificar() throws NoContinuaException {
        try {
            this.lookupConfiguracionBalanceSBBean().update(this.panel.getData());
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }
    
    public void nuevo() throws NoContinuaException {
        try {
            this.lookupConfiguracionBalanceSBBean().create(this.panel.getData());
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }
    
    public void eliminar() throws NoContinuaException {
        try {
            this.lookupConfiguracionBalanceSBBean().delete((ConFlujosFormuladosDTO) this.getTO());
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }
    
    public void salir() throws NoContinuaException {
    }
    
    public void imprimir() throws NoContinuaException {
    }
    
    public Object getTO() throws NoContinuaException {
        return null;
    }
    
    public void cancelar() throws NoContinuaException {
    }
    
    public String getNombreModelo() {
        return nombreModelo;
    }
    
    public void setNombreModelo(String nombreModelo) {
        this.nombreModelo = nombreModelo;
    }
}
